package B1;
 import B1.kettle;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;




public class run {
    private static Integer time=0;
    private static Integer temp=0;
    public static void main(String[] args) {
        kettle kettle=new kettle();
        System.out.println("开始启动...");
        kettle.setStatus(false);//电路正常
        kettle.setOn(true);//插电
        kettle.setIswater(true);//有水
        kettle.setTemperature(temp=15);//设置当前温度
        if(isDamage(kettle)){
            System.out.println("电路损坏，不能加热！");
        }else{
            heating(kettle);
        }
    }
    public static boolean isOn(kettle kettle){
        System.out.println("恒温水壶是否启动："+(kettle.isOn()?"是":"否"));
        return kettle.isOn();
    }
    public static boolean isWater(kettle kettle){
        System.out.println("是否有水："+(kettle.isWater()? "有" : "无"));
        return kettle.isWater();
    }
    public static boolean isDamage(kettle kettle) {
        System.out.println("是否有损坏："+(kettle.isStatus()? "有" : "无"));
        return kettle.isStatus();
    }
    public static void heating(kettle kettle){
        if(isOn(kettle)){
            if(isWater(kettle)){
                System.out.println("正在加热...");
                ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
                Runnable task = ()->{
                    temp++;
                    System.out.println("加热度数："+temp+"°C");
                    if(temp>=20){
                        scheduler.shutdown();//关闭定时器
                        temp=20;
                        System.out.println("\n加热完成！");
                        kettle.setTemperature(temp);
                        kettle.setOn(false);//断开电源
                        //重写isOn方法，判断是否断开电源
                        isOn(kettle);
                    }
                };
                scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
            }else{
                System.out.println("没有水，不能加热！");
            }
        }else{
            System.out.println("电源已断开！");
        }
    }
}